/*
Lambda
File: Vector2.cpp
Desc: Source file that contains the definition for Vector2 class
Author: Srinath - Sep 2010
*/

#include "Vector.h"

/*
Function    : Constructor
Params      : None
Desc        : Default constructor
Return type : None
*/
Vector2::Vector2()
{
	//set both x,y to 0.0f
	x = 0.0f;
	y = 0.0f;
}

/*
Function    : Constructor
Params	    : @1 - float val1
			  @2 - float val2
Desc		: Initialize vector with explicit values
Return type : None
*/
Vector2::Vector2(float _x, float _y)
{
	x = _x;
	y = _y;
}

/*
Function	: Length
Params		: None
Return type : float
Desc		: Returns the length of the vector
*/
float Vector2::Length()
{
	return sqrtf(x*x + y*y);
}

/*
Function	: SquaredLength
Params		: None
Return type	: float
Desc		: Function calculates the square of the length of vector and returns it
*/
float Vector2::SquaredLength()
{
	return (x*x + y*y);
}

/*
Function	: Normalize
Params		: None
Return type	: void
Desc		: Normalize the given vector ie make its length 1
*/
void Vector2::Normalize()
{
	float inv_length = 1/sqrtf(x*x + y*y);
	x *= inv_length;
	y *= inv_length;
}

/*
Function	: GetNormalizedVector
Params		: None
Return type	: Vector2
Desc		: Get a normalized vector using data from this vector
*/
Vector2 Vector2::GetNormalizedVector() const
{
	float inv_length = 1/sqrtf(x*x + y*y);
	return Vector2(x*inv_length, y*inv_length);
}

/*
Function	: Operator - (unary)
Params		: None
Return type	: Vector2
Desc		: Returns a negated vector using this vector
*/
Vector2 Vector2::operator - ()
{
	return Vector2(-x, -y);
}

/*
Function	: Operator + (binary)
Params		: @1 - Vector2&
Return type	: Vector2
Desc		: Adds two vectors
*/
Vector2 Vector2::operator + (Vector2& V)
{
	return Vector2(x+V.x, y+V.y);
}

/*
Function	: Operator * (binary)
Params		: @1 - float(scalar)
Return type	: Vector2
Desc		: Scales the vector by scalar 
*/
Vector2 Vector2::operator * (float k)
{
	return Vector2(k*x, k*y);
}

/*
Function	: Operator / (binary)
Params		: @1 - float(scalar)
Return type	: Vector2
Desc		: Scales a vector by inverse of scalar ie divides the vector by scalar
*/
Vector2 Vector2::operator / (float k)
{
	float inv_k = 1/k;
	return Vector2(x*inv_k, y*inv_k);
}

/*
Function	: Operator * (binary)
Params		: @1 - Vector2
Return type	: float
Desc		: Function computes the dot product of two vectors
*/
float Vector2::operator * (Vector2& V)
{
	return (x*V.x + y*V.y);
}

/*
Function	: Operator ^ (binary)
Params		: @1 - Vector2
Return Type	: float
Desc		: Computes the cross product of two vectors.
			  Description about the cross product of two vectors is given in Wolfram Mathematica. For further details check out the website
*/
float Vector2::operator ^ (Vector2& V)
{
	return (x*V.y - y*V.x);
}


